- title 'Admin bunker'
- content_for :head do
  = stylesheet_link_tag 'admin', :media => 'all'
  = javascript_include_tag '//cdn.ably.io/lib/ably.min.js'
- content_for :title do
  %h1 Admin bunker
%p
  %h2 Stats
  %ul
    %li= link_to "Users: #{@stats[:user_count]}", admin_data_path('user')
    %li= link_to "Users signed up within last 14 days: #{@stats[:user_count_within_14_days]}", admin_data_path('user')
    %li= link_to "Accounts: #{@stats[:account_count]}", admin_data_path('account')
    %li= link_to "Backlogs: #{@stats[:backlog_count]}", admin_data_path('backlog')
    %li= link_to "Stories: #{@stats[:story_count]}", admin_data_path('story')
    %li= link_to "Pending invites: #{@stats[:pending_invites]}", admin_data_path('invited_user')
    %li= link_to "Beta Signups (deprecated): #{@stats[:beta_signups]}", admin_data_path('beta_signup')
    %li= link_to "Sidekiq", '/bunker/sidekiq/'

  %h2 Exports
  %ul
    %li= link_to "All registered users", admin_export_path('all_users'), :target => "_blank"
    %li= link_to "All accounts", admin_export_path('all_accounts'), :target => "_blank"
    %li= link_to "Beta Signups (deprecated)", admin_export_path('beta_signup'), :target => "_blank"
    %li= link_to "Users applied for beta but never signed up, older than 7 days", admin_export_path('not_signed_up_7_days'), :target => "_blank"
    %li= link_to "Users signed up that have not used easyBacklog in 7 days", admin_export_path('not_used_7_days'), :target => "_blank"

  %h2 People currently editing backlogs
  %table.people
    %tr.head
      %th Name
      %th Accounts
      %th Backlog ID
      %th Editing since

  %h2 Recent cron jobs
  %table.cron-jobs
    %tr.head
      %th Message
      %th Run at
    - @recent_cron_logs.each do |cron|
      %tr
        %td{:title => cron.info}= cron.message
        %td= cron.created_at.strftime('%H:%M %d/%m/%Y')

:javascript
  var ably = new Ably.Realtime({
    authUrl: '/realtime-token',
    log: { level: 5 }
  });

  var table = $('table.people');
  function addPerson(member) {
    var tr = table.find('tr#member-' + member.clientId);
    if (tr.empty()) {
      tr = $('<tr id="member-' + member.clientId + '">');
      table.append(tr);
    }
    tr.append($('<td>').text(member.data.name));
    var $accounts = $('<td>');
    for (var accountIndex = 0; accountIndex < member.data.accounts.length; accountIndex++) {
      var account = member.data.accounts[accountIndex];
      $accounts.append($('<a href="/bunker/data/account?filter=' + escape(account) + '">').text(account));
      if (accountIndex < member.data.accounts.length - 1) { $accounts.append(', '); }
    }
    tr.append($accounts);
    tr.append($('<td>').text(member.data.backlogId));
    tr.append($('<td class="time">').text(new Date(member.data.since)));
  }

  function removePerson(member) {
    table.find('tr#member-' + member.clientId).remove();
  }

  var globalPresent = ably.channels.get('global-editors').presence;
  globalPresent.subscribe('enter', addPerson);
  globalPresent.subscribe('update', addPerson);
  globalPresent.subscribe('present', addPerson);
  globalPresent.subscribe('leave', removePerson);
